$(document).ready(function() {
	$('#all').click(function() {
        $.each($('input[type=checkbox]'),function() {
            this.checked = true;
        });
    });
    
    if(document.getElementById('type') != null && $('#type').val() == '网站顶部广告') {
    	$('input.bn').attr('onclick', "window.open('"+$('#app').val()+"/Public/upload/type/banner/t/1', 'pic', 'width=400,height=150,top=200,left=420')");
    } else if(document.getElementById('type') != null && $('#type').val() == '网站轮播广告') {
    	$('input.bn').attr('onclick', "window.open('"+$('#app').val()+"/Public/upload/type/banner/t/2', 'pic', 'width=400,height=150,top=200,left=420')");
    }
    
    $('#cancel').click(function() {
        $.each($('input[type=checkbox]'),function() {
            this.checked = false;
        });
    });
    
   
    $('input[name=type]').click(function() {
    	if(this.value == 1) {
    	   $('input.bn').attr('onclick', "window.open('"+$('#app').val()+"/Public/upload/type/banner/t/1', 'pic', 'width=400,height=150,top=200,left=420')");
    	} else if(this.value == 2) {
    	   $('input.bn').attr('onclick', "window.open('"+$('#app').val()+"/Public/upload/type/banner/t/2', 'pic', 'width=400,height=150,top=200,left=420')");
    	}
    });
    
    
    /**
     * add操作
     */
    
    $('form.add input[name=name]').focus(function() {
        $('form.add span.name.info').show();
        $('form.add span.name.success').hide();
        $('form.add span.name.error').hide().html('');
    }).blur(function() {
        $('form.add span.name.info').hide();
        checkName(this);
    });
    
    function checkName(_this) {
        if($(_this).val().length < 2 || $(_this).val().length > 30) {
            $('form.add span.name.error').show().html('广告名称必须是2~30位');
            $('form.add #flag').val('false');
            return ;
        } else {
            $('form.add span.name.success').show();
            $('form.add #flag').val('true');
        }
        
    }
    
    $('form.add input[name=link]').focus(function() {
        $('form.add span.link.info').show();
        $('form.add span.link.success').hide();
        $('form.add span.link.error').hide().html('');
    }).blur(function() {
        $('form.add span.link.info').hide();
        checkUrl(this);
    });
    
    function checkUrl(_this) {
        if($(_this).val() == '') {
            $('form.add span.link.error').show().html('url链接不能为空!');
            $('form.add #flag').val('false');
            return ;
        } else {
        	if(!/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/.test($(_this).val())) {
        		 $('form.add span.link.error').show().html('url链接不合法!');
                 $('form.add #flag').val('false');
                 return false;
        	} else {
        		$('form.add span.link.success').show();
	            $('form.add #flag').val('true');
        	}
        }
    }
    
    $('form.add input[name=pic]').focus(function() {
        $('form.add span.pic.info').show();
        $('form.add span.pic.success').hide();
        $('form.add span.pic.error').hide().html('');
    }).blur(function() {
        $('form.add span.pic.info').hide();
        checkPic(this);
    });
    
    function checkPic(_this) {
        if($(_this).val() == '') {
            $('form.add span.pic.error').show().html('图片必须上传!');
            $('form.add #flag').val('false');
            return ;
        } else {
            $('form.add span.pic.success').show();
        }
    }
    
    $('form.add').submit(function() {
        var name = $('form.add input[name=name]');
        var url = $('form.add input[name=link]');
        var pic = $('form.add input[name=pic]');
        $('form.add span.pic.error').hide();
        checkName(name);
        checkUrl(url);
        checkPic(pic);
        if($('form.add #flag').val() != 'true') return false;
    });

});

//打开上传窗口
function up() {
    window.open($('#app').val()+'/Public/upload/type/banner/t/1', 'pic', 'width=400,height=150,top=200,left=420');
}


